package cn.bluemobi.app.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import cn.bluemobi.app.entity.output.ShoppingCart;
import cn.bluemobi.app.entity.output.ShoppingCartItem;
import cn.bluemobi.app.mapper.ShoppingCartMapper;
import cn.bluemobi.app.service.ShoppingCartService;
import cn.bluemobi.common.core.dto.Dto;
import cn.bluemobi.common.core.dto.ListDto;

@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {

	@Autowired
	private ShoppingCartMapper shoppingCartMapper;

	@Override
	public Dto<Object> addShoppingCart(String memberId, String commodityId,
			String specificationId, String quantity) {
		Dto<Object> dto = new Dto<Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("memberId", memberId);
		params.put("commodityId", commodityId);
		params.put("specificationId", specificationId);
		params.put("quantity", quantity);
		if (shoppingCartMapper.checkIsExistShoppingCart(params) > 0) {
			shoppingCartMapper.updateShoppingCart(params);
		} else {
			shoppingCartMapper.insertShoppingCart(params);
		}
		return dto;
	}

	@Override
	public Dto<Object> updateShoppingCartQuantity(String memberId,
			String shoppingCartIds, String quantitys) {
		Dto<Object> dto = new Dto<Object>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<String> shoppingCartIdList = Arrays.asList(shoppingCartIds
				.split(","));
		List<String> quantityList = Arrays.asList(quantitys.split(","));
		for (int i = 0; i < shoppingCartIdList.size(); i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("shoppingCartId", shoppingCartIdList.get(i));
			map.put("quantity", quantityList.get(i));
			list.add(map);
		}
		shoppingCartMapper.updateShoppingCartQuantity(list);
		return dto;
	}

	@Override
	public Dto<Object> deleteShoppingCarts(String memberId,
			String shoppingCartIds) {
		Dto<Object> dto = new Dto<Object>();
		List<String> shoppingCartIdList = Arrays.asList(shoppingCartIds
				.split(","));
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("memberId", memberId);
		params.put("shoppingCartIdList", shoppingCartIdList);
		shoppingCartMapper.deleteShoppingCarts(params);
		return dto;
	}

	/**
	 * 分组 Description: <br/>
	 * 
	 * @author lvxh
	 * @param groupList
	 * @return
	 */
	private Map<String, List<ShoppingCartItem>> groupList(
			List<ShoppingCartItem> groupList) {
		Map<String, List<ShoppingCartItem>> groupMap = new HashMap<String, List<ShoppingCartItem>>();
		if (!CollectionUtils.isEmpty(groupList)) {
			for (ShoppingCartItem shoppingCartItem : groupList) {
				if (groupMap.containsKey(shoppingCartItem.getMerchantId())) {
					List<ShoppingCartItem> list = groupMap.get(shoppingCartItem
							.getMerchantId());
					list.add(shoppingCartItem);
				} else {
					List<ShoppingCartItem> list = new ArrayList<ShoppingCartItem>();
					list.add(shoppingCartItem);
					groupMap.put(shoppingCartItem.getMerchantId(), list);
				}
			}
		}
		return groupMap;
	}

	@Override
	public ListDto<ShoppingCart> findShoppingCartList(String memberId) {
		ListDto<ShoppingCart> dto = new ListDto<ShoppingCart>();
		List<ShoppingCart> shoppingCarts = new ArrayList<ShoppingCart>();
		List<ShoppingCartItem> shoppingCartItems = shoppingCartMapper
				.findShoppingCartList(memberId);
		if (!CollectionUtils.isEmpty(shoppingCartItems)) {
			Map<String, List<ShoppingCartItem>> shoppingCartItemGroupMap = groupList(shoppingCartItems);
			for (String key : shoppingCartItemGroupMap.keySet()) {
				ShoppingCart shoppingCart = shoppingCartMapper
						.findMerchantInfo(key);
				shoppingCart.setShoppingCartItems(shoppingCartItemGroupMap
						.get(key));
				shoppingCarts.add(shoppingCart);
			}
		}
		dto.setList(shoppingCarts);
		return dto;
	}
}
